/* $Id: EditEntryPanel.java 137 2011-08-29 20:33:36Z altdotua@gmail.com $ */

package alt.djudge.frontend.client.ui.grids;

import java.util.List;
import java.util.Vector;

import alt.djudge.frontend.client.ui.editors.CustomEditor;
import alt.djudge.frontend.client.ui.editors.EditorsFactory;
import alt.djudge.frontend.shared.dto.admin.AdminDTOEntry;

import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.Widget;


public class EditEntryPanel<T extends AdminDTOEntry> extends FlowPanel
{
	Grid grid;
	
	public List<CustomEditor> textBoxes = new Vector<CustomEditor>();
	
	public final boolean fCreate;
	
	public final DTOEntryGridWrapper<T> entry;
	
	public EditEntryPanel(DTOEntryGridWrapper<T> entry)
	{
		this(entry, false);
	}
	
	public CustomEditor getEditor(int i)
	{
		return textBoxes.get(i);
	}
	
	public EditEntryPanel(final DTOEntryGridWrapper<T> entry, final boolean fCreate)
	{
		this.fCreate = fCreate;
		this.entry = entry;
		
		String[] fieldNames = entry.dtoEntry.getFieldNames();
		int fieldsCount = fieldNames.length;
		grid = new Grid(0, fieldsCount);
		
		for (int i = 0; i < fieldsCount; i++)
		{
			grid.insertRow(i);
			grid.setText(i, 0, fieldNames[i]);
			
			CustomEditor editor = EditorsFactory.getEditor(entry.dtoEntry.getClass(), i);
			grid.setWidget(i, 1, (Widget) editor);
			
			editor.setKey(entry.dtoEntry.getFieldValue(i));
			textBoxes.add(editor);
			if (i == 0)
			{
				editor.setEnabled(false);
			}
		}
		
		add(grid);
	}
}
